﻿using System;
using System.Data;

public partial class Currency : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    private void ShowPanel(string panelName)
    {
        #region Hide panels
        emptyPanel.Visible = false;

        allListPanel.Visible = false;
        editEntryPanel.Visible = false;
        addNewEntryPanel.Visible = false;

        #endregion Hide panels

        try
        {
            #region Get Panel Name
            DataTable countryList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.CountryList);

            switch (panelName)
            {

                case "allListPanel":
                    allListPanel.Visible = true;

                    DataTable sourceDataTable = Utils.ModuleMain().GetCurrencyList();
                    listGridView.DataSource = sourceDataTable;
                    listGridView.DataBind();
                    break;               

                case "emptyPanel":
                    emptyPanel.Visible = true;
                    break;

                case "addNewEntryPanel":
                    addNewEntryPanel.Visible = true;                    
                    Utils.FillSelector(addNewCountryDDL, countryList, "Name", "Code");
                    break;

                case "editEntryPanel":
                    Utils.FillSelector(editCountryDropDownList, countryList, "Name", "Code");
                    editEntryPanel.Visible = true;
                    break;

                default:
                    emptyPanel.Visible = true;
                    break;
            }

            #endregion Get Panel Name

        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (mServerObject == null) mServerObject = new ServerObject();

        string areaGUID = string.Empty;
        if (Request["area"] != null)
        {
            areaGUID = Request["area"].ToString();
        }

        #region Display Panels depending of area

        if (Utils.AutentificatedUser)
        {
            if (!IsPostBack)
            {
                string areaName = areaGUID;

                switch (areaName)
                {
                    case "allList":
                        ShowPanel(allListPanel.ID);
                        break;

                    default:
                        ShowPanel(allListPanel.ID);
                        break;
                }
            }
        }
        else
        {
            ShowPanel(string.Empty);
        }

        #endregion Display Panels depending of area
    }

    protected void newButton_Click(object sender, EventArgs e)
    {
        ClearControlAddNewEntryPanel();
        ShowPanel(addNewEntryPanel.ID);
    }

    protected void addNewCancelButton_Click(object sender, EventArgs e)
    {
        ClearControlAddNewEntryPanel();
        ShowPanel(allListPanel.ID);
    }   

    protected void addNewSaveButton_Click(object sender, EventArgs e) //todo: impliment id from selector
    {
        try
        {
            string name = addNewNameTextBox.Text.Trim();
            string code = addNewCodeTextBox.Text.Trim();
            string country = addNewCountryDDL.SelectedValue;
            int countryID = 0;
            int.TryParse(country, out countryID);

            string symbol = addNewSymbolTextBox.Text.Trim();

            if (Utils.ModuleMain().AddCurrency(name, code, countryID, symbol))
            {
                ClearControlAddNewEntryPanel();
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea cu internet ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in sistem!", ex.Message.ToString()); }
    }

    protected void listGridView_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        try
        {
            int index = e.NewEditIndex;
            ShowPanel(editEntryPanel.ID);
            entryIDLabel.Text = listGridView.Rows[index].Cells[0].Text;
            editNameTextBox.Text = listGridView.Rows[index].Cells[1].Text;
            editCodeTextBox.Text = listGridView.Rows[index].Cells[2].Text;
            editCountryDropDownList.SelectedValue = listGridView.Rows[index].Cells[3].Text;
            editSymbolTextBox.Text = listGridView.Rows[index].Cells[5].Text;
        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }        
    }

    protected void editSaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            string strTypeID = entryIDLabel.Text;
            int entryID = 0;
            int.TryParse(strTypeID, out entryID);

            string name = editNameTextBox.Text.Trim();            
            string code = editCodeTextBox.Text.Trim();
            string country = editCountryDropDownList.SelectedValue;
            int countryID = 0;
            int.TryParse(country, out countryID);
            string symbol = editSymbolTextBox.Text.Trim();

            if (Utils.ModuleMain().UpdateCurrency(entryID, name, code, countryID, symbol))
            {
                ClearControlEditPanel();
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " { " + name + " } - nu a fost salvat. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in sistem!", ex.Message.ToString()); }
    }

    protected void listGridView_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;

        try
        {
            string strTableID = listGridView.Rows[index].Cells[0].Text;
            int tableID = 0;
            int.TryParse(strTableID, out tableID);

            if (Utils.ModuleMain().DeleteCurrency(tableID))
            {
                ShowPanel(allListPanel.ID);
            }
            else
            {
                Utils.InfoText(this, "Atentie", " Stergerea valutei nu a fost efectuata. Verificati conectiunea internetului ");
            }
        }
        catch (Exception ex)
        { Utils.InfoText(this, "Eroare in sistem!", ex.Message.ToString()); }
    }

    protected void editCancelButton_Click(object sender, EventArgs e)
    {
        ClearControlEditPanel();
        ShowPanel(allListPanel.ID);
    }

    protected void listGridView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        listGridView.PageIndex = e.NewPageIndex;
        ShowPanel(allListPanel.ID);
    }

    #region ClearControls

    private void ClearControlAddNewEntryPanel()
    {
        addNewNameTextBox.Text = string.Empty;       
        addNewCodeTextBox.Text = string.Empty;
        addNewCountryDDL.SelectedIndex = -1;
        addNewSymbolTextBox.Text = string.Empty;
    }

    private void ClearControlEditPanel()
    {
        editNameTextBox.Text = string.Empty;        
        editCodeTextBox.Text = string.Empty;
        editCountryDropDownList.SelectedIndex = -1;
        editSymbolTextBox.Text = string.Empty;
    }

    #endregion CleaControls

}